package tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.studerb.model.Header;
import com.studerb.model.NewsGroup;
import com.studerb.service.interfaces.IHeaderService;
import com.studerb.service.interfaces.INewsGroupService;

public class TestHibernateLazy {

	private static Logger logger = Logger.getLogger("TESTS");
	private static ClassPathXmlApplicationContext springAppContext;
	private static IHeaderService headerService;
	private static INewsGroupService newsGroupService;
	private static String groupName = "1.binaries.pictures.erotica";

	/*
	 * private static void loadHeaderThreads() throws Exception { StopWatch
	 * stopWatch = new StopWatch(); stopWatch.start(); try { headerThreads =
	 * session.createQuery("from HeaderThread").list(); transaction.commit(); }
	 * catch (Exception e) { if (transaction != null) { transaction.rollback();
	 * throw e; } } finally { session.close(); } stopWatch.stop();
	 * logger.debug("Time: " + stopWatch.toString());
	 * 
	 * NewsGroup newsGroup = headerThreads.get(0).getNewsGroup();
	 * logger.debug("NewsGroup id: " + newsGroup.getId());
	 * logger.debug("Newsgroup name: " + newsGroup.getGroupName()); }
	 */

	private static List<Header> createHeaders(NewsGroup newsGroup) throws IOException {
		ArrayList<Header> headers = new ArrayList<Header>();
		String fileName = "classpath:tests/headers.txt";
		File headerFile = springAppContext.getResource(fileName).getFile();
		BufferedReader reader = new BufferedReader(new FileReader(headerFile));
		String currentLine;
		while ((currentLine = reader.readLine()) != null) {
			Header tempHeader = Header.createHeader(currentLine, newsGroup);
			if (tempHeader != null) {
				headers.add(tempHeader);
			}
			if (headers.size() > 20) {
				break;
			}
		}
		logger.debug("CREATED: " + headers.size() + " headers");
		return headers;

	}

	public static void main(String args[]) {
		NewsGroup newsGroup;
		com.jidesoft.utils.Lm.verifyLicense("Bill Studer", "Gnusbin", "GUPxWo:0hpUtR3u94.K90:R7sPr.VCv2");
		System.out.println("Starting");
		try {
			setUpSpring();
			newsGroup = newsGroupService.getByGroupName(groupName);
			List<Header> headers = createHeaders(newsGroup);
			headerService.saveHeaders(headers, newsGroup);
			headerService.deleteAllPosts(newsGroup);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		finally {
			springAppContext.close();
		}

		System.exit(0);
	}

	private static void setUpSpring() {
		logger.debug("Starting Spring Application Context");
		springAppContext = new ClassPathXmlApplicationContext(new String[] { "testContext.xml" });
		headerService = (IHeaderService) springAppContext.getBean("headerService");
		newsGroupService = (INewsGroupService) springAppContext.getBean("newsGroupService");
		springAppContext.registerShutdownHook();
	}

}
